package com.maiji.cloud.scheduled;

import com.maiji.cloud.entities.community.ComMsgInfo;
import com.maiji.cloud.entities.login.UploadRecord;
import com.maiji.cloud.microservice.UploadRecordService;
import com.maiji.cloud.service.CommunityService;
import com.maiji.cloud.utils.BaiduAI;
import org.apache.commons.lang.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class MaijiAsyncService {

    Logger logger = LoggerFactory.getLogger(MaijiAsyncService.class);
    @Autowired
    private CommunityService communityService;
    @Autowired
    private UploadRecordService uploadRecordService;

    @Async
    public void examineImgAsync (String msgId) {
        List<UploadRecord> msgImgs = uploadRecordService.findAllByBelongIds(Arrays.asList(msgId));
        if (msgImgs.size() > 0) {
            for (UploadRecord img: msgImgs) {
                try {
                    Thread.sleep(555);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Boolean examineImg = BaiduAI.examineImg(img.getUrl());
                if (examineImg == null) return; // 审核图片有报错，跳过，下次在审核
                if (BooleanUtils.isFalse(examineImg)) {
                    communityService.updateById(new ComMsgInfo().setMsgId(msgId).setIsolation(true).setExamined(1));
                    logger.info("审核动态图片 ===>> 拒绝 ===>> imgId = {}", img.getUrl());
                    return;
                }
            }
            communityService.updateById(new ComMsgInfo().setMsgId(msgId).setIsolation(false).setExamined(1));
            logger.info("审核动态图片 ===>> 通过 ===>> msgId = {}", msgId);
        }
    }

}
